# This permission handler should be included into any class that can handle permissions
# It allows you to call a method such as "can_destroy_post?" and it will be passed onto the base_permissions method
# You can also override the "can" methods individually for more controller

# REFACTORME Think of a better name than PermissionHandler
module PermissionHandler
  def method_missing(call, *args)
    if call.to_s =~ /^can_(\w+)_(\w+)\?$/
      base_permissions $1, $2.to_sym, args.first
    else
      super
    end
  end
  
  # This method is meant to be overridden if necessary
  # REFACTORME Think of a better name than base_permissions
  def base_permissions(action, item_type, item = nil)
    false
  end
  
  def can?(action, item_type, item)
    send("can_#{action}_#{item_type}?", item)
  end
end
